package com.deng.etl.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.deng.etl.common.IEtlInterface;
import com.deng.etl.common.EtlLog;
//删除、导入
public class ImportEmployee extends ImportBase implements IEtlInterface {
	
	public static SimpleDateFormat format_l=new SimpleDateFormat("yyyy-dd-HH hh:mm:ss:SSS");
	
	public static String COLUMNS="人员码,名字,生日,科室,身份证,职级,类别,操作员,操作日期";
	
	public static String SELECT="select "+COLUMNS+"  from neusoft.JIXIAO_EMPLOYEE";
	
	public static String INSERT="insert into t_sys_stat_employee (name,his_id,create_time,modify_time) values(?,?,?,?)";
	
	public static String UPDATE="update t_sys_stat_employee set name=?,modify_time=? where his_id=?";	
	
	public static String DELETE="delete from t_sys_stat_employee";
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
	}
	
	public void insertData(Connection sourceConn,Connection targetConn,String name,String fromDate,String toDate) {
		System.out.println("从："+fromDate+"   到："+toDate);
		
		try {
			EtlLog.progressOneTaskETLLog(targetConn, name, null);//启动日志
			PreparedStatement pst=sourceConn.prepareStatement(SELECT);
			
			PreparedStatement pst_insert=targetConn.prepareStatement(INSERT);
//			PreparedStatement pst_update=CONN.prepare(conn_target, UPDATE);
			ResultSet rs=pst.executeQuery();
			while(rs.next()){
				String or_code=rs.getString("人员码");
				boolean exist=checkExist(targetConn, or_code);
				//修改
				if(exist){
				}
				else{//新增  name,his_id,create_time,modify_time
					pst_insert.setString(1, rs.getString("名字"));
					pst_insert.setString(2, rs.getString("人员码"));
					Date date=new Date();
					pst_insert.setTimestamp(3, getTimestamp(date));
					pst_insert.setTimestamp(4, getTimestamp(date));
					insert(pst_insert);
				}
			}
			rs.close();
			pst.close();
			pst_insert.close();
//			pst_update.close();
			EtlLog.finishOneTaskETLLog(targetConn, name, toDate);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void deleteData(Connection sourceConn, Connection targetConn,
			String name, String fromDate, String toDate) {
		// TODO Auto-generated method stub
		System.out.println("从："+fromDate+"   到："+toDate);
		try{
			EtlLog.progressOneTaskETLLog(targetConn, name, null);//启动日志
			PreparedStatement pst_delete=targetConn.prepareStatement(DELETE);
			pst_delete.executeUpdate();
			pst_delete.close();
				
			EtlLog.finishOneTaskETLLog(targetConn, name, toDate);
		}
		catch(SQLException e){
			
		}
		
	}

	public void updateData(Connection sourceConn, Connection targetConn,
			String name, String fromDate, String toDate) {
		try{
			EtlLog.progressOneTaskETLLog(targetConn, name, null);//启动日志
			
			deleteData(sourceConn, targetConn,
					name, fromDate, toDate);
			insertData(sourceConn, targetConn,
					name, fromDate, toDate);
			
			EtlLog.finishOneTaskETLLog(targetConn, name, toDate);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		
	}
	
	public boolean checkExist(Connection conn,String code){
		boolean ret=false;
		try {
			PreparedStatement pst =conn.prepareStatement("select his_id from t_sys_stat_employee where his_id='"+code+"'");
			ResultSet rs=pst.executeQuery();
			if(rs.next()){
				ret=true;
			}
			rs.close();
			pst.close();
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ret;
	}
	
	public static Timestamp getTimestamp(java.util.Date date){

		return Timestamp.valueOf(format_l.format(date));
	}
	
}
